#include "public_def.h"

#include "sdp.h"
#include "h264ser.h"
#include "rtspser.h"
#include "utils.h"
#include "public_def.h"


//Generate Session Descriptor Protocol string
//
//Parameter:
//	conn				RTSP connection
//	buf					Output string buffer
//Return:
//	0 succ.
//
int GenSDPSessionDescr(RTSPConnection_t * pConn, char * pBuf)
{
	HI_CHInfo_t * info = g_ptRTSPServer->m_tChnInfo;
	info += pConn->chID;

	if (info)
	{
		char base64VPS[512];
		char base64SPS[512];
		char base64PPS[512];
		char base64PPS2[512];

		//unsigned char sps[] = {0x67, 0x42, 0xe0, 0x1e, 0xda, 0x02, 0xc0, 0x49, 0x10, 0x68, 0xce, 0x30, 0xa4, 0x80};
		//base64_encode(sps, sizeof(sps), base64, sizeof(base64));
		
		sprintf(pBuf, "v=0" CRLF);
		sprintf(pBuf, "%so=- %u %u IN IP4" , pBuf, pConn->sdpSessionID, pConn->sdpVersionID);
		sprintf(pBuf, "%s %s" CRLF, pBuf, pConn->localIP);
		sprintf(pBuf, "%ss=SDP Descrption" CRLF, pBuf);
		sprintf(pBuf, "%si=SDP Description" CRLF, pBuf);
		sprintf(pBuf, "%sa=type:broadcast" CRLF, pBuf);
		sprintf(pBuf, "%sa=tool:DIGIBIRD Streaming Media" CRLF, pBuf);
		sprintf(pBuf, "%sa=x-qt-text-nam:session descriped" CRLF, pBuf);
		sprintf(pBuf, "%sa=x-qt-text-inf:1" CRLF, pBuf);
		sprintf(pBuf, "%se=nightwatcher@126.com" CRLF, pBuf);
		sprintf(pBuf, "%sc=IN IP4 %s" CRLF, pBuf, pConn->remoteip);
		sprintf(pBuf, "%st=0 0" CRLF, pBuf);
		sprintf(pBuf, "%sa=control:*" CRLF, pBuf);
		sprintf(pBuf, "%sa=range:npt=0-" CRLF, pBuf);
		sprintf(pBuf, "%sa=x-broadcastcontrol:TIME" CRLF, pBuf);
		sprintf(pBuf, "%sa=x-copyright: DIGIBIRD" CRLF, pBuf);
		sprintf(pBuf, "%sc=IN IP4 0.0.0.0" CRLF, pBuf);
		//DVI4, static code ADPCM DVI4 5
		//dbgprintf(0, "%s", pBuf);

		switch (info->m_iVideoFormat)
		{
		case RTSP_SERVER_FORMAT_MJPEG:
			//sprintf(pBuf, "%sm=video %d RTP/AVP %d" CRLF, pBuf, pConn->devCh->RTPVPort, RTP_PT_JPEG);
			sprintf(pBuf, "%sm=video 0 RTP/AVP %d" CRLF, pBuf, RTP_PT_JPEG);
			break;
		case RTSP_SERVER_FORMAT_H264:
			//sprintf(pBuf, "%sm=video %d RTP/AVP %d" CRLF, pBuf, pConn->devCh->RTPVPort, RTP_PT_H264);
			sprintf(pBuf, "%sm=video %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_H264);
			sprintf(pBuf, "%sa=rtpmap:%d H264/90000" CRLF, pBuf, RTP_PT_H264);
			if((info->spslen > 0) &&  (info->ppslen > 0))
			{
				base64_encode1(info->sps, info->spslen, base64SPS, sizeof(base64SPS));
				base64_encode1(info->sps +info->spslen, info->ppslen, base64PPS, sizeof(base64PPS));
				sprintf(pBuf, "%sa=fmtp:%d packetization-mode=1;profile-level-id=", pBuf, RTP_PT_H264);
				if(info->pps2len)
				{
					base64_encode1(info->sps +info->spslen +info->ppslen , info->pps2len, base64PPS2, sizeof(base64PPS2));
					sprintf(pBuf, "%s%02X%02X%02X;sprop-parameter-sets=%s,%s,%s" CRLF, pBuf, info->sps[1], info->sps[2], info->sps[3], base64SPS, base64PPS, base64PPS2);
				}
				else
				{
					sprintf(pBuf, "%s%02X%02X%02X;sprop-parameter-sets=%s,%s" CRLF, pBuf, info->sps[1], info->sps[2], info->sps[3], base64SPS, base64PPS);
				}

				//sprintf(pBuf, "%sa=fmtp:96 packetization-mode=1;profile-level-id=42E014;sprop-parameter-sets=%s"CRLF, pBuf, base64);
			}

			break;
		case RTSP_SERVER_FORMAT_H265:
			//sprintf(pBuf, "%sm=video %d RTP/AVP %d" CRLF, pBuf, pConn->devCh->RTPVPort, RTP_PT_H264);
			sprintf(pBuf, "%sm=video %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_H264);
			sprintf(pBuf, "%sa=rtpmap:%d H265/90000" CRLF, pBuf, RTP_PT_H264);
			if ((info->vpslen > 0) && (info->spslen > 0) && (info->ppslen > 0))
			{
				base64_encode1(info->sps, info->vpslen, base64VPS, sizeof(base64VPS));
				base64_encode1(info->sps + info->vpslen, info->spslen, base64SPS, sizeof(base64SPS));
				base64_encode1(info->sps + info->vpslen + info->spslen, info->ppslen, base64PPS, sizeof(base64PPS));
				sprintf(pBuf, "%sa=fmtp:%d sprop-vps=%s;sprop-sps=%s;sprop-pps=%s" CRLF, pBuf, RTP_PT_H264, base64VPS, base64SPS, base64PPS);

				//sprintf(pBuf, "%sa=fmtp:96 packetization-mode=1;profile-level-id=42E014;sprop-parameter-sets=%s"CRLF, pBuf, base64);
			}

			break;
		}
		//sprintf(pBuf, "%sb=AS:3840" CRLF, pBuf);
		sprintf(pBuf, "%sa=recvonly" CRLF, pBuf);
		sprintf(pBuf, "%sa=control:trackID=2" CRLF, pBuf);
#if 1
		dbgprintf(0, "%s %d info->iSampleRate = %d ", __func__, __LINE__, info->iSampleRate);
		if(info->iSampleRate > 0)
		{
			switch (info->m_iAideoFormat)
			{
			case RTSP_SERVER_FORMAT_G711A:
				sprintf(pBuf, "%sm=audio %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_ALAW);
				sprintf(pBuf, "%sa=control:trackID=1" CRLF, pBuf);
				sprintf(pBuf, "%sa=rtpmap:%d PCMA/%d/1"CRLF, pBuf, RTP_PT_ALAW, info->iSampleRate);
				break;
			case RTSP_SERVER_FORMAT_G711Mu:
				sprintf(pBuf, "%sm=audio %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_ULAW);
				sprintf(pBuf, "%sa=control:trackID=1" CRLF, pBuf);
				sprintf(pBuf, "%sa=rtpmap:%d PCMU/%d/1"CRLF, pBuf, RTP_PT_ULAW, info->iSampleRate);
				break;
			case RTSP_SERVER_FORMAT_ADPCM:
				//sprintf(pBuf, "%sm=audio %d RTP/AVP %d" CRLF, pBuf, pConn->devCh->RTPAPort, RTP_PT_DVI4);
				sprintf(pBuf, "%sm=audio %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_DVI4);
				sprintf(pBuf, "%sa=control:trackID=1" CRLF, pBuf);
				break;
			case RTSP_SERVER_FORMAT_AAC:
				sprintf(pBuf, "%sm=audio %d RTP/AVP %d" CRLF, pBuf, 0, RTP_PT_AAC);
				sprintf(pBuf, "%sa=control:trackID=1" CRLF, pBuf);
				//sprintf(pBuf, "%sb=AS:%d"CRLF, pBuf,RTP_PT_AAC);
				sprintf(pBuf, "%sa=rtpmap:%d MPEG4-GENERIC/%d/%d"CRLF, pBuf,RTP_PT_AAC,info->iSampleRate, info->iNumChannel);
				sprintf(pBuf, "%sa=fmtp:%d streamtype=5;profile-level-id=15;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=%s;Profile=1;"CRLF, pBuf,RTP_PT_AAC,info->fConfig);

				break;
			}
		}
#endif
		//sprintf(pBuf, "%sa=fmtp:26 packetization-mode=1;profile-level-id=", pBuf);
		//sprintf(pBuf, "%s%02X%02X%02X;sprop-parameter-sets=%s" CRLF, pBuf, info->sps[0], info->sps[1], info->sps[2], base64);
	}
    
	return 0;
}



